import { create } from 'zustand'
import { CreateStaffQrcodeForm, StaffInfo, CustomerRecognitionConfig, ApprovalTypeEnum } from './interface'

/**
 * 表单验证规则
 */
export const rules = {
  name: [
    { required: true, message: '请输入活码名称' }
  ],
  welcomeMsg: [
    { required: true, message: '请输入欢迎语' }
  ]
}

const defaultFormValue: CreateStaffQrcodeForm = {
  name: '',
  staffList: [],
  welcomeMsg: '',
  approvalType: ApprovalTypeEnum.AUTO,
  enableTags: true,
  tags: '',
  enableRemark: true,
  remarkTemplate: '',
  enableAvatar: true,
  avatar: '',
  enablePoster: true,
  poster: '',
  enableRecConfig: true,
  recConfig: {
    enablePageStye: true,
    pageStye: '',
    oldCustomerType: '',
    scanCodeToOpen: '',
    tags: ''
  }
}

// 页面状态接口
interface CreateStaffQrcodeState {
  // 表单数据
  formData: CreateStaffQrcodeForm
  // 是否正在提交
  submitting: boolean
  // 是否已初始化
  isInited: boolean
}

// Store接口
interface CreateStaffQrcodeStore extends CreateStaffQrcodeState {
  // 更新表单数据
  setFormData: (data: Partial<CreateStaffQrcodeForm>) => void
  // 更新员工列表
  setStaffList: (staffList: StaffInfo[]) => void
  // 更新标签列表
  setTags: (tags: '') => void
  // 更新海报配置
  setPoster: (poster: string) => void
  // 设置提交状态
  setSubmitting: (submitting: boolean) => void
  // 设置初始化状态
  setIsInited: (isInited: boolean) => void
  // 重置状态
  reset: () => void
}

// 初始状态
const initialState: CreateStaffQrcodeState = {
  formData: defaultFormValue,
  submitting: false,
  isInited: false
}

// 创建store
const useCreateStaffQrcodeStore = create<CreateStaffQrcodeStore>((set) => ({
  ...initialState,

  // 更新表单数据
  setFormData: (data) => set((state) => ({
    formData: {
      ...state.formData,
      ...data
    }
  })),

  // 更新员工列表
  setStaffList: (staffList) => set((state) => ({
    formData: {
      ...state.formData,
      staffList
    }
  })),

  // 更新标签列表
  setTags: (tags) => set((state) => ({
    formData: {
      ...state.formData,
      tags
    }
  })),

  // 更新海报配置
  setPoster: (poster) => set((state) => ({
    formData: {
      ...state.formData,
      poster
    }
  })),

  // 设置提交状态
  setSubmitting: (submitting) => set({ submitting }),

  // 设置初始化状态
  setIsInited: (isInited) => set({ isInited }),

  // 重置状态
  reset: () => set(initialState)
}))

// 导出状态更新方法
export const setFormData = (data: Partial<CreateStaffQrcodeForm>) =>
  useCreateStaffQrcodeStore.setState((state) => ({
    formData: {
      ...state.formData,
      ...data
    }
  }))

export const setStaffList = (staffList: StaffInfo[]) =>
  useCreateStaffQrcodeStore.setState((state) => ({
    formData: {
      ...state.formData,
      staffList
    }
  }))

export const setTags = (tags: '') =>
  useCreateStaffQrcodeStore.setState((state) => ({
    formData: {
      ...state.formData,
      tags
    }
  }))

export const setPoster = (poster: string) =>
  useCreateStaffQrcodeStore.setState((state) => ({
    formData: {
      ...state.formData,
      poster
    }
  }))

export const setSubmitting = (submitting: boolean) =>
  useCreateStaffQrcodeStore.setState({ submitting })

export const setIsInited = (isInited: boolean) =>
  useCreateStaffQrcodeStore.setState({ isInited })

export const resetStore = () =>
  useCreateStaffQrcodeStore.setState(initialState)

export default useCreateStaffQrcodeStore
